package opennlp.ccg;

import java.awt.List;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;

import opennlp.ccg.grammar.Grammar;
import opennlp.ccg.lexicon.DynamicLexicon;
import opennlp.ccg.parse.ParseException;
import opennlp.ccg.parse.Parser;
import opennlp.ccg.synsem.Sign;
import opennlp.maxent.MaxentModel;
import opennlp.maxent.io.SuffixSensitiveGISModelReader;
import supertagger.io.XMLPOSDictionaryReader;
import supertagger.io.XMLWordDictionaryReader;
import supertagger.tagger.MaxEntSupertagger;
import supertagger.tagger.WordAndPOSDictionaryLabellingStrategy;
import supertagger.util.STTaggerPOSDictionary;
import supertagger.util.STTaggerWordDictionary;

public class ParsingApplication {
	public static void main(String[] args) {
		String usage = "java opennlp.ccg.TextCCG " +
		"(<grammarfile>) | (-exportprefs <prefsfile>) | (-importprefs <prefsfile>) (-supertagger <supertaggermodelfile>)";

		if (args.length > 0 && args[0].equals("-h")) {
			System.out.println("Usage: " + usage);
			System.exit(0);
		}

//		args
		String grammarfile = "grammar.xml";
		String supertagmodelfile = null;
		String inputFile = "";
		
		boolean supertag = false;

		for (int i = 0; i < args.length; i++) {			
			if (args[i].equals("-supertagger")) {
				supertag = true; supertagmodelfile = args[++i]; continue;
			}
			if (args[i].equals("-grammar")) {
				grammarfile = args[++i]; continue;	
			}
			if(args[i].equals("-input")) {
				inputFile = args[++i]; continue;
			}		
		}
		
//		 load grammar
        URL grammarURL = null;
		try {
			grammarURL = new File(grammarfile).toURL();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        System.out.println("Loading grammar from URL: " + grammarURL);
        Grammar grammar = null;
		try {
			grammar = new Grammar(grammarURL);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        if (grammar.getName() != null)
            System.out.println("Grammar '" + grammar.getName() + "' loaded.");
        
        // Build the supertagger.
        
        STTaggerWordDictionary wd = null; int K = 20;
        STTaggerPOSDictionary pd = null;
        double[] betas = {0.001, 0.0001, 0.00001, 0.000001, 0.0000001};
        
        int nextBeta = 1;
        int lastBeta = betas.length-1;
        
        XMLWordDictionaryReader wdr = new XMLWordDictionaryReader("../lib/openccgWD.xml");
        wd = wdr.read();
        XMLPOSDictionaryReader posdr = new XMLPOSDictionaryReader("../lib/openccgWD.xml");
        pd = posdr.read();
        MaxentModel tagModel = null;
		try {
			tagModel = new SuffixSensitiveGISModelReader(new File(supertagmodelfile)).getModel();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        WordAndPOSDictionaryLabellingStrategy labstrat = new WordAndPOSDictionaryLabellingStrategy(wd, pd, K);
        DynamicLexicon dynomite = 
        	new DynamicLexicon(grammar.lexicon, new MaxEntSupertagger(tagModel, labstrat, betas[0]));
                
        System.out.println();
        
        // create parser and realizer
        Parser parser = new Parser(grammar);
        // Turn on supertagger-based lexical lookup constraints.
        // Slip in the supertagger-driven lexicon.
        parser.dynLex = dynomite; 
        parser.setDynamicity(true);     
		
        BufferedReader bf = null;
		try {
			bf = new BufferedReader(new FileReader(new File(inputFile)));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		String line = "";
		try {
			line = bf.readLine();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		int parsed = 0, notParsed = 0, total = 0;
		while(line!=null) {
			total++;
			
			try {
				parser.parse(line);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			Collection<Sign> signs = parser.getRankedResult();
			
			if(signs.size()==0) {
				DynamicLexicon dl = null;
				notParsed++;
				System.out.println("--------------------------------------");
				System.out.println("No parses found for sentence:\n"+line);
				System.out.println("--------------------------------------");
			} else {
				System.out.println("--------------------------------------");
				parsed++;
			}
			
			int i = 0;
			for(Sign sg: signs) {
				i++;
				System.out.println("Result #"+i+":\n"+sg.getCategory().getLF().toString());
				if(i==signs.size())
					System.out.println("--------------------------------------");
			}
		}

	}
}
